package com.yellowpage.white.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import com.yellowpage.white.dao.QueryAssistantHQL;



public class QueryAssistantHQL {

	private String querySql; 

	private ArrayList otherConditionList;
	private ArrayList conditionList;
	private List paremsList;

	public static String DATA_TYPE_STRING = "STRING";
	public static String DATA_TYPE_DATE = "DATE";
	private List dataTypeList = null;
	
	private int from = -1;
	
	private int to = -1;

	public QueryAssistantHQL(String sql) {
		this.querySql = sql;
		this.paremsList = new ArrayList();
		this.conditionList = new ArrayList();
		this.dataTypeList = new ArrayList();
	}

	public QueryAssistantHQL addCondition(String condition) {
		this.addCondition(condition, this.DATA_TYPE_STRING);

		return this;
	}

	public QueryAssistantHQL addCondition(String condition, String type) {
		this.conditionList.add(condition);
		this.paremsList.add(getParemName(condition));
		this.dataTypeList.add(type);
		return this;
	}

	private String getParemName(String condition) {
		String lastStr = condition.substring(condition.indexOf(":") + 1);
		return lastStr.substring(0, lastStr.indexOf(" "));
	}
	
	public List query(Session session, Map parems){
		List valueList = this.query(session, parems, null);
		return valueList;
	}

	public List query(Session session, Map parems, ArrayList otherCondition) {
		List valueList = new ArrayList();
		for (int i = 0; i < paremsList.size(); i++) {
			String parem = (String) paremsList.get(i);
			String type = (String) dataTypeList.get(i);
			String value;
			if (parems.get(parem) != null) {
				if (type.equals(this.DATA_TYPE_DATE)) {
					Date date = (Date) parems.get(parem);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					value = sdf.format(date);
				} else {
					value = (String) parems.get(parem);
				}
				if (value != null && value.length() > 0) {
					String condition = (String) conditionList.get(i);
					this.querySql += " "
							+ condition.replaceAll(":" + parem, "'" + value
									+ "'");

				}
			}

		}
		if (otherCondition != null) {
			for (int j = 0; j < otherCondition.size(); j++) {
				this.querySql += (String) otherCondition.get(j);
			}
		}
		System.out.println(this.querySql);

		try {
			Query query = session.createQuery(this.querySql);
			if(this.from != -1 && this.to !=-1){
				query.setFirstResult(from);
				query.setMaxResults(to);
			}
			
			valueList = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

		return valueList;
	}

	/**
	 * 
	 * getter and setter
	 */

	public String getQuerySql() {
		return querySql;
	}

	public void setQuerySql(String querySql) {
		this.querySql = querySql;
	}

	public int getFrom() {
		return from;
	}

	public void setFrom(int from) {
		this.from = from;
	}

	public int getTo() {
		return to;
	}

	public void setTo(int to) {
		this.to = to;
	}
	
}
